package com.xiaobao.coalproperty.module.user.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.module.user.model.entity.EntrustListEntity;
import com.xiaobao.coalproperty.other.adapter.UserEntrustAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.DialogUtils;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class EntrustActivity extends BaseActivity {
    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private UserEntrustAdapter mUserEntrustAdapter;

    private int page = 1;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("委托管理");

        mUserEntrustAdapter = new UserEntrustAdapter(this);
        mUserEntrustAdapter.setOnItemClickListener(mOnItemClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mUserEntrustAdapter);
        update();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            page = 1;
            update();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            update();
            mXRecyclerView.loadMoreComplete();
        }
    };

    private UserEntrustAdapter.onItemClickListener mOnItemClickListener = new UserEntrustAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {

        }

        @Override
        public void onUpdateClick(View view, int position) {
            Intent intent = new Intent(EntrustActivity.this, SaleOrderActivity.class);
            intent.setAction("id");
            intent.putExtra("id", mUserEntrustAdapter.getAdapterData().get(position).getId());
            startActivity(intent);
        }

        @Override
        public void onOffClick(View view, final int position) {
            DialogUtils.createConfirmDialog(EntrustActivity.this, "提示", "确定要放弃委托吗？", "确定", "取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    offEntrust(mUserEntrustAdapter.getAdapterData().get(position).getId());
                }
            },null,-1).show();
        }
    };

    private void offEntrust(String id){
        OkHttpUtils.post().url(UrlConstant.URL_SETASSET_ENTRUST)
                .params(HttpHelperUtils.getInstance().setAssetEntrustParams(id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if(entity.getCode()==0){
                        page = 1;
                        update();
                    }
                }catch (Exception e){}
            }
        });
    }

    private void update() {
        OkHttpUtils.post().url(UrlConstant.URL_ASSET_ENTRUST_LIST)
                .params(HttpHelperUtils.getInstance().assetEntrustListParams(page + ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    EntrustListEntity entity = new Gson().fromJson(response, EntrustListEntity.class);
                    if (entity.getCode() == 0) {
                        if (entity.getData().getList().size() > 0) {
                            if (page == 1) {
                                mUserEntrustAdapter.setAdapterData(entity.getData().getList());
                            } else {
                                mUserEntrustAdapter.addAdapterData(entity.getData().getList());
                            }
                            page++;
                        } else {
                            showToast("没有更多了");
                        }
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }
}
